package com.yanqu.road.server.manager.activity.twins;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.twins.activity.CrossTwinsActivity;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTwinsMgr extends TempCrossMgr {

    public final static Map<Integer, CrossTwinsActivityGroup> activityGroupMap = new ConcurrentHashMap<>();

    public static synchronized CrossTwinsActivity getActivity(int activityId,int groupId) {
        UnionActivityGroup group = Cross2UnionActivityGroupMgr.getGroupData(activityId,groupId);
        if (group == null){
            return null;
        }
        if (activityGroupMap.get(activityId) == null) {
            synchronized (activityGroupMap){
                if (activityGroupMap.get(activityId) == null){
                    // 读配置 获取活动信息 保证有活动信息
                    ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
                    if (activityInfo == null || activityInfo.getType() != eActivityType.Twins.getValue()) {
                        return null;
                    }
                    CrossTwinsActivityGroup activityGroup = new CrossTwinsActivityGroup(activityInfo);
                    activityGroupMap.put(activityId, activityGroup);
                }
            }
        }
        CrossTwinsActivityGroup activityGroup = activityGroupMap.get(activityId);
        return activityGroup.getActivity(groupId);
    }

    public static Map<Integer, CrossTwinsActivityGroup> getActivityGroupMap() {
        return activityGroupMap;
    }



    @Override
    public boolean save() {
        for (CrossTwinsActivityGroup activityGroup : activityGroupMap.values()) {
            for (CrossTwinsActivity activity : activityGroup.getActivityMap().values()) {
                activity.save();
            }
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        getLogger().info("start reload Twins activity  start");

        // 获取活动
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.Twins.getValue());
        Map<Integer, ActivityInfo> tempActivityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
        List<Integer> activityIdList = new ArrayList<>();
        for (ActivityInfo activityInfo : tempActivityInfoMap.values()) {
            Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
            if (null != activityGroupMap){
                for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                    if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()){
                        getActivity(activityInfo.getActivityId(),activityGroup.getGroupId());
                        break;
                    }
                }
            }
        }
        getLogger().info("reload Twins activity end");
        return true;
    }


    public boolean removeExpireData(){
        List<Integer> removeList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for(int activityId : activityGroupMap.keySet()){
            for (CrossTwinsActivity activity : activityGroupMap.get(activityId).getActivityMap().values()){
                if(activity.getActivity() == null || (nowTime > activity.getActivity().getEndShowTime()  * 1000 + 3 * 24 * 3600 * 1000)){
                    removeList.add(activityId);
                }
                break;
            }
        }
        for(int activityId : removeList){
            activityGroupMap.remove(activityId);
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
